package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.zzw.TCatalogue;
import com.ruoyi.common.core.domain.zzw.VO.TCatalogueTree;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TCatalogueMapper;
import com.ruoyi.system.service.ITCatalogueService;

/**
 * 目录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-03
 */
@Service
public class TCatalogueServiceImpl implements ITCatalogueService 
{
    @Autowired
    private TCatalogueMapper tCatalogueMapper;

    /**
     * 查询目录
     * 
     * @param cCatalogueId 目录主键
     * @return 目录
     */
    @Override
    public TCatalogue selectTCatalogueByCCatalogueId(Long cCatalogueId)
    {
        return tCatalogueMapper.selectTCatalogueByCCatalogueId(cCatalogueId);
    }

    /**
     * 查询目录列表
     * 
     * @param tCatalogue 目录
     * @return 目录
     */
    @Override
    public List<TCatalogueTree> selectTCatalogueList(TCatalogue tCatalogue)
    {
        List<TCatalogue> tCatalogues = tCatalogueMapper.selectTCatalogueList(tCatalogue);
        List<TCatalogueTree> list=tCatalogues.stream().filter(item->item.getcParentId()==0)
                .map(ite->creeteTree(ite,tCatalogues))
                .collect(Collectors.toList());
        return list;
    }

    private TCatalogueTree creeteTree(TCatalogue item, List<TCatalogue> list) {
        TCatalogueTree tree=new TCatalogueTree();
        BeanUtils.copyProperties(item,tree);
        tree.setChildren(new ArrayList<>());
        //过滤出当前节点的直接子节点
        List<TCatalogue> collect = list.stream().filter(node -> node.getcParentId() == item.getcCatalogueId())
                .collect(Collectors.toList());
        //递归添加子节点
        for (TCatalogue tCatalogue : collect) {
            TCatalogueTree creeteTree = creeteTree(tCatalogue, list);
            tree.getChildren().add(creeteTree);
        }
        return tree;
    }

    /**
     * 新增目录
     * 
     * @param tCatalogue 目录
     * @return 结果
     */
    @Override
    public int insertTCatalogue(TCatalogue tCatalogue)
    {
        return tCatalogueMapper.insertTCatalogue(tCatalogue);
    }

    /**
     * 修改目录
     * 
     * @param tCatalogue 目录
     * @return 结果
     */
    @Override
    public int updateTCatalogue(TCatalogue tCatalogue)
    {
        return tCatalogueMapper.updateTCatalogue(tCatalogue);
    }

    /**
     * 批量删除目录
     * 
     * @param cCatalogueIds 需要删除的目录主键
     * @return 结果
     */
    @Override
    public int deleteTCatalogueByCCatalogueIds(Long[] cCatalogueIds)
    {
        return tCatalogueMapper.deleteTCatalogueByCCatalogueIds(cCatalogueIds);
    }

    /**
     * 删除目录信息
     * 
     * @param cCatalogueId 目录主键
     * @return 结果
     */
    @Override
    public int deleteTCatalogueByCCatalogueId(Long cCatalogueId)
    {
        return tCatalogueMapper.deleteTCatalogueByCCatalogueId(cCatalogueId);
    }
}
